package cn.fastoo.web.backstage.web.controller.product;

import cn.fastoo.common.dao.product.ProductCategoryDao;
import cn.fastoo.common.dao.product.query.ViewProductCategoryAttrgroupAttrDao;
import cn.fastoo.common.dao.product.query.ViewProductCategoryAttrgroupAttrValueDao;
import cn.fastoo.common.dao.product.query.ViewProductCategoryAttrgroupDao;
import cn.fastoo.common.entity.product.Product;
import cn.fastoo.common.entity.product.ProductCategory;
import cn.fastoo.common.entity.product.Query.ViewProductCategory;
import cn.fastoo.common.entity.product.Query.ViewProductCategoryAttrgroup;
import cn.fastoo.common.entity.product.Query.ViewProductCategoryAttrgroupAttr;
import cn.fastoo.common.entity.product.Query.ViewProductCategoryAttrgroupAttrValue;
import cn.fastoo.common.sampleframe.SFBeanUtils;
import cn.fastoo.common.sampleframe.dto.JsonResult;
import cn.fastoo.common.sampleframe.search.QuerySearch;
import cn.fastoo.common.sampleframe.search.Search;
import cn.fastoo.common.sampleframe.springside.Collections3;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * Created by lowkey on 17/10/11.
 */
@Controller
@RequestMapping("/product/category")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryDao dao;

    @RequestMapping
    public String index() {
        return "/product/category/index";
    }

    @RequestMapping("/category-attrgroup-attr-value")
    public String all(){
        return "/product/category-attrgroup-attr-value";
    }


    @RequestMapping("/manager")
    @ResponseBody
    public String manager(@ModelAttribute ProductCategory newData, QuerySearch querySearch, String op) {
        Search search = querySearch.getSearch();
        if(StringUtils.isNotBlank(op)){
            if("add".equals(op)){
                if(dao.findOne(new Search().addSearchFilter("EQ_name",newData.getName()))!=null){
                    return JsonResult.error("category name already exists");
                }
                dao.insert(newData);
                //logger.info(loginUser, SystemLog.TYPE_AUTH_ROLE_ADD,String.format("用户角色[%s]",authRole.getRoleName()));
            }else {
                ProductCategory oldData = dao.findById(newData.getId());
                if(oldData==null){
                    return JsonResult.success("错误参数,请检查");
                }
                if("save".equals(op)){
                    newData.setVersion(oldData.getVersion());
                    dao.update(newData);
                }else if("del".equals(op)){
                    dao.delete(oldData);
                }
            }
            return JsonResult.success("操作成功");
        }
        return JsonResult.querySearchSuccess(dao.find(search),querySearch);
    }







}
